package LuoGu.贪心.合并果子;

import java.util.PriorityQueue;
import java.util.Scanner;

//优先队列
public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        long ans = 0;
        PriorityQueue<Long> queue = new PriorityQueue<>();
        for(int i=0;i<n;i++){
            long temp = s.nextLong();
            queue.offer(temp);
        }
        while(queue.size()!=1){
            long a = queue.poll();
            long b = queue.poll();
            ans+=a+b;
            System.out.println(a+b);
            queue.offer(a+b);
        }
        System.out.print(ans);
    }
}
